package com.neuedu.it.teach.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 互助数据格式化工具类
 * 用于将数据库查询到的互助信息格式化为易读的文本
 */
public class HelpDataFormatter {

    /**
     * 格式化互助信息列表
     * @param pendingHelps 待处理的互助信息列表
     * @return 格式化后的文本
     */
    public static String formatPendingHelps(List<Map<String, Object>> pendingHelps,String localhost) {
        if (pendingHelps == null || pendingHelps.isEmpty()) {
            return "当前暂无待处理的互助信息，请回答用户'暂无待处理的互助信息，请过段时间再来看看，感谢您对乡村教育事业的关心与支持！'";
        }
        
        StringBuilder result = new StringBuilder("当前数据库中待处理的学生需求的助教信息如下，请根据这些数据回答用户问题：\n");
        
        for (Map<String, Object> help : pendingHelps) {
            result.append("\n【").append(help.get("title")).append("】\n");
            // result.append("互助ID：").append(help.get("help_id")).append("\n");
            result.append("跳转URL：").append(localhost).append("/home/detail/").append(help.get("help_id")).append("\n");
            // result.append("发布者ID：").append(help.get("user_id")).append("\n");
            result.append("发布者用户名：").append(help.get("user_name")).append("\n");
            result.append("学生姓名：").append(help.get("full_name")).append("\n");
            // result.append("学生ID：").append(help.get("student_id")).append("\n");
            // online为线上，offline为线下
            String gender = (String) help.get("gender");
            if ("Male".equalsIgnoreCase(gender)) {
                gender = "男";
            } else if ("Female".equalsIgnoreCase(gender)) {
                gender = "女";
            }
            result.append("性别：").append(gender).append("\n");
            result.append("年龄：").append(help.get("age")).append("\n");
            result.append("学校：").append(help.get("school")).append("\n");
            result.append("年级：").append(help.get("grade")).append("\n");
            result.append("科目：").append(help.get("subject")).append("\n");
            // online为线上，offline为线下
            String helpType = (String) help.get("help_type");
            if ("online".equalsIgnoreCase(helpType)) {
                helpType = "线上";
            } else if ("offline".equalsIgnoreCase(helpType)) {
                helpType = "线下";
            }

            result.append("助教类型：").append(helpType).append("\n");
            result.append("地点：").append(help.get("location")).append("\n");
            result.append("频率：").append(help.get("frequency")).append("\n");
            result.append("时长：").append(help.get("duration")).append("\n");
            result.append("志愿奖励：").append(help.get("reward")).append("\n");
            result.append("联系方式：").append(help.get("user_phone")).append("\n");
            // result.append("状态：").append(help.get("status")).append("\n");
            
            // 格式化创建时间
            Object createdAtObj = help.get("created_at");
            if (createdAtObj != null) {
                String createdAt;
                if (createdAtObj instanceof Date) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    createdAt = sdf.format((Date) createdAtObj);
                } else {
                    createdAt = createdAtObj.toString();
                }
                result.append("发布时间：").append(createdAt).append("\n");
            }
            
            result.append("详细说明：").append(help.get("description")).append("\n");
        }
        
        return result.toString();
    }
    
    /**
     * 构建完整的对话内容
     * @param pendingHelps 待处理的互助信息列表
     * @return 格式化后的JSON字符串，包含系统提示、助手回复和用户问题的占位
     */
    public static String buildDialogueContent(List<Map<String, Object>> pendingHelps, String localhost) {
        return formatPendingHelps(pendingHelps,localhost);
    }

}